package com.hms.service.impl;

import org.springframework.stereotype.Service;

import com.hms.dao.IBaseDao;
import com.hms.dao.IResourceDao;
import com.hms.entity.Resource;
import com.hms.service.IResourceService;

@Service
public class ResourceService extends BaseService<Resource, String> implements IResourceService {

	@javax.annotation.Resource
	private IResourceDao resourceDao;

	@Override
	public IBaseDao<Resource, String> getDao() {
		return resourceDao;
	}

	@Override
	public String save(Resource entity) {
		String id = resourceDao.save(entity);
		resourceDao.flush();
		this.flushSpringSecurity();
		return id;
	}

	@Override
	public void update(Resource entity) {
		resourceDao.update(entity);
		resourceDao.flush();
		flushSpringSecurity();
	}

	@Override
	public void delete(Resource entity) {
		resourceDao.delete(entity);
		resourceDao.flush();
		flushSpringSecurity();
	}

	@Override
	public void delete(String id) {
		Resource resource = resourceDao.load(id);
		this.delete(resource);
	}

	@Override
	public void delete(String[] ids) {
		for (String id : ids) {
			this.delete(id);
		}
	}

	/**
	 * 刷新springSecurity
	 */
	private void flushSpringSecurity() {
		// try {
		// FactoryBean factoryBean = (FactoryBean)
		// SpringUtil.getBean("&adminSecurityDefinitionSource");
		// FilterInvocationDefinitionSource filterInvocationDefinitionSource =
		// (FilterInvocationDefinitionSource) factoryBean.getObject();
		// FilterSecurityInterceptor filterSecurityInterceptor =
		// (FilterSecurityInterceptor)
		// SpringUtil.getBean("filterSecurityInterceptor");
		// filterSecurityInterceptor.setObjectDefinitionSource(filterInvocationDefinitionSource);
		// }
		// catch (Exception e) {
		// e.printStackTrace();
		// }
	}

}
